import java.util.Iterator;

public class ArraySet<Item> extends MySet<Item> {

    Item elements[];
    int initialLength = 10;

    public ArraySet(){
        elements = (Item[])new Object[initialLength];
    }

    private void resize(int max){
        Item[] temp = (Item[]) new Object[max];
        for (int i = 0; i < size; i++) {
            temp[i]=elements[i];
        }
        elements=temp;
    }

    @Override
    public Iterator<Item> iterator() {
        return new ArraySetIterator();
    }

    final class ArraySetIterator implements Iterator<Item>{

        int i=size;

        @Override
        public boolean hasNext() {
            return i>0;
        }

        @Override
        public Item next() {
            return elements[--i];
        }
    }

    @Override
    public void addElement(Item item) {
        if(this.contains(item)){
            return;
        }
        if(size==elements.length){
            resize(2*elements.length);
        }
        elements[size++]=item;
    }

    @Override
    public boolean contains(Object o) {
        for (int i = 0; i < size; i++) {
            if(elements[i].equals(o)){
                return true;
            }
        }
        return false;
    }

}
